<template>
  <div class="com_container">
    <div class="title" :style="comTitleStyle">
      <span class="title_s">{{ '▎  ' + showTitle }}</span>
      <span class="iconfont icon-arrow-down" @click="isShow = !isShow" :style="comTitleStyle"></span>
      <div class="title_warp" :style="marginLeft" v-for="item in selectTypes" :key="item.key" v-show="isShow" @click="handleType(item.key)">
        <div class="titleItem" :style="comTitleStyle">{{ item.text }}</div>
      </div>
    </div>
    <div class="com_chart" ref="ref_trend"></div>
  </div>
</template>

<script>
import { getThemeValue } from '@/utils/theme_utils'

import { mapState } from 'vuex'
export default {
  data() {
    return {
      echartInstance: null,
      allData: null,
      // 是否显示可选项
      isShow: false,
      // 默认选择map项标题
      selectTypeKey: 'map',
      titleAdapter: 0
    }
  },
  computed: {
    // 获得title展示数组
    selectTypes() {
      // return this.allData.type
      if (!this.allData) {
        return []
      } else {
        return this.allData.type.filter((i) => {
          return i.key !== this.selectTypeKey
        })
      }
    },
    // 获取点击后selectType的值渲染 到span标签
    showTitle() {
      if (!this.allData) {
        return ''
      } else {
        return this.allData[this.selectTypeKey].title
      }
    },
    comTitleStyle() {
      return {
        fontSize: this.titleAdapter + 'px',
        color: getThemeValue(this.theme).titleColor
      }
    },
    marginLeft() {
      return {
        marginLeft: this.titleAdapter + 'px'
      }
    },
    ...mapState(['theme'])
  },

  methods: {
    initCharts() {
      this.echartInstance = this.$echarts.init(this.$refs.ref_trend, this.theme)
      // this.chartInstane = this.$echarts.init(this.$refs.trend_ref, this.theme)
      // console.log(this.theme)
      // console.log(this.$refs.chart) // 绘制图表
      const initOption = {
        xAxis: [
          {
            type: 'category',
            boundaryGap: false
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        // 位置
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          top: '30%',
          containLabel: true
        },
        // legend 位置
        legend: {
          top: '15%',
          left: 20,
          icon: 'circle'
        },
        // 提示
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        }
      }
      this.echartInstance.setOption(initOption)
    },
    async getData() {
      const { data: res } = await this.$http.get('trend')
      // console.log(res)
      this.allData = res

      this.upDataChart()
    },
    upDataChart() {
      // 半透明的颜色值
      const colorArr1 = ['rgba(11, 168, 44, 0.5)', 'rgba(44, 110, 255, 0.5)', 'rgba(22, 242, 217, 0.5)', 'rgba(254, 33, 30, 0.5)', 'rgba(250, 105, 0, 0.5)']
      // 全透明的颜色值
      const colorArr2 = ['rgba(11, 168, 44, 0)', 'rgba(44, 110, 255, 0)', 'rgba(22, 242, 217, 0)', 'rgba(254, 33, 30, 0)', 'rgba(250, 105, 0, 0)']
      // 处理数据
      // x轴
      const timeArr = this.allData.common.month
      // y轴
      const valueArr = this.allData[this.selectTypeKey].data
      const seriesArr = valueArr.map((item, index) => {
        return {
          type: 'line',
          name: item.name,
          data: item.data,
          stack: this.selectTypeKey,
          // 区域颜色
          areaStyle: {
            // 渐变色
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: colorArr1[index] },
              { offset: 1, color: colorArr2[index] }
            ])
          }
        }
      })
      // legend数据
      const legendArr = valueArr.map((item) => {
        return item.name
      })

      const dataOption = {
        xAxis: {
          data: timeArr
        },
        yAxis: {},
        series: seriesArr,
        legend: {
          data: legendArr
        }
      }
      this.echartInstance.setOption(dataOption)
    },
    screenAdapter() {
      this.titleAdapter = (this.$refs.ref_trend.offsetWidth / 100) * 3.6
      const adapterOptions = {
        legend: {
          itemWidth: this.titleAdapter,
          itemHeight: this.titleAdapter,
          itemGap: this.titleAdapter,
          textStyle: {
            fontSize: this.titleAdapter / 1.6
          }
        }
      }
      this.echartInstance.setOption(adapterOptions)

      this.echartInstance.resize()
    },
    // 切换标题点击事件
    handleType(currentType) {
      console.log(currentType)
      this.selectTypeKey = currentType
      this.isShow = false
      this.upDataChart()
    }
  },
  mounted() {
    this.initCharts()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },

  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
  },
  watch: {
    theme() {
      console.log('主题改变了')
      console.log(this.theme)

      this.echartInstance.dispose() // 销毁当前的图表
      this.initCharts() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.upDataChart() // 更新图表的展示
    }
  }
}
</script>

<style lang="scss" scoped>
.com_container {
  position: relative;
  .title {
    position: absolute;
    top: 20px;
    left: 20px;
    z-index: 4;
    color: #ffffff;

    .iconfont {
      margin-left: 10px;
      cursor: pointer;
    }
    // .title_warp {
    //   background-color: #222733;
    // }
    .titleItem {
      padding-top: 2px;
      cursor: pointer;
    }
  }
}
</style>
